﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GMS.Model;
using System.Collections.Generic;//for list
using GMS.Utils;
using System.Text.RegularExpressions;
//using GMS.Utils.ServiceContext;
using GMS.BLL;

public partial class UserControl_ComQueryControls : System.Web.UI.UserControl
{
    public DataSet dsData = new DataSet();
    ComFunctions CFs = new ComFunctions();
    ManageControls MC = new ManageControls();
   
    public Dictionary<string, TabColumns> tabColumns = new Dictionary<string, TabColumns>();
    public string sQueryCls = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (dsData.Tables.Count <= 0)
            GetViewStateData();
        else
        {
            ViewState["dsData"] = dsData;
            ViewState["tabColumns"] = tabColumns;
        }
        LoadControl(dsData, tabColumns);
    }

    /// <summary>
    ///获取回发时存在ViewState里面数据 
    /// </summary>
    protected void GetViewStateData()
    {
        dsData = (DataSet)ViewState["dsData"];
        tabColumns = (Dictionary<string, TabColumns>)ViewState["tabColumns"];
    }

    /// <summary>
    /// 查询控件自动生成
    /// </summary>
    /// <param name="dsData"></param>
    public void LoadControl(DataSet dsData, Dictionary<string, TabColumns> tabColumns)
    {
        try
        {
            tbQueryCls.InnerHtml = "";
            int iType = 0, iColNum = 0, iDisNum = 4;
            string SelId = string.Empty;
            string SelName = string.Empty;
            string strValue = string.Empty;
            DataTable dtTables = new DataTable();
            Dictionary<string, TabColumns> relation = new Dictionary<string, TabColumns>();
            //Dictionary<string, TabColumns> tabColumns = CDO.GetTabColFromCDO();

            tbQueryCls.InnerHtml = "<tr>";
            txtQueryCls.Text = "";
            foreach (GMS.Model.TabColumns tabCol in tabColumns.Values)
            {
                if (tabCol.COLSEARCH == 1)
                {
                    iColNum++;
                    if (iColNum % iDisNum == 0)
                        tbQueryCls.InnerHtml += "</tr><tr>";
                    iType = tabCol.COLTYPE;

                    if (tabCol.FOREIGNTAB != "") //表示关联的，那么肯定是下拉框的
                    {
                        iType = 9;
                        dtTables = dsData.Tables[tabCol.COLCODE];

                        //if (dtTables == null) //应该是二级类型(多级）
                        {   //多级类型的时候，子级的table名表示方法：父级字段名-子级字段名
                            for (int j = 0; j < dsData.Tables.Count; j++)
                            {
                                if (dsData.Tables[j].TableName.ToUpper().Contains("-" + tabCol.COLCODE.ToUpper()))
                                {
                                    //dtTables = dsData.Tables[j];
                                    dtTables = null;
                                    break;
                                }
                                else if (dsData.Tables[j].TableName.ToUpper().Contains(tabCol.COLCODE.ToUpper() + "-"))  //父级获得所有父级子级数据
                                {
                                    dtTables = dsData.Tables[j];
                                    iType = 10;
                                    break;
                                }
                            }
                        }
                        //

                        SelId = tabCol.FOREIGNCOL;// dtTitles.Rows[i]["FOREIGNCOL"].ToString();
                        SelName = tabCol.FOREIGNSHOWCOL;// dtTitles.Rows[i]["FOREIGNSHOWCOL"].ToString();
                    }
                    //string maxlength = tabCol.MAXLENGTH.ToString();//dtTitles.Rows[i]["MAXLENGTH"].ToString();
                    this.tbQueryCls.InnerHtml += MC.DisplayQuerys(iType, false, tabCol.COLCODE, tabCol.COLNAME, "", dtTables, "", SelId, SelName);
                    if (iType == (int)EnumHelper.E_ColDataType.COL_DATE)
                    {
                        iColNum++;
                        txtQueryCls.Text += "S_Cls-" + tabCol.COLCODE + "-" + iType.ToString() + ";";
                        txtQueryCls.Text += "E_Cls-" + tabCol.COLCODE + "-" + iType.ToString() + ";";
                    }
                    else
                        txtQueryCls.Text += "Cls-" + tabCol.COLCODE + "-" + iType.ToString() + ";";
                }
            }
            tbQueryCls.InnerHtml += "</tr>";
            sQueryCls = txtQueryCls.Text;
        }
        catch (Exception ex)
        {
            this.Page.RegisterClientScriptBlock("toAlert", "<script>alert('查询控件加载出错');</script>");//关闭窗口
        }
    }
}
